package com.fhnew.app.ui.activity.recomend.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResgetMoney;
import com.fhnew.app.model.network.entity.req.Respromotion;

import com.fhnew.app.util.SYSOUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/25.
 * 我的推荐
 */
public class MyrecommendFragment extends Fragment {

    private TextView myyongjin;
    private TextView shareuserid;
    private TextView todeyadd;
    private TextView addnumberuser;
    private TextView todeyteamadd;
    private TextView teamaddnumber;
    private TextView yestodayget;
    private TextView historymoney;
    private TextView cangetmoney;
    private TextView getmoney;
    private TextView getmoneyurl;
    private TextView copeshareurl;
    private TextView copyyaoqingma;

    private TextView yaoqingma;

    private ClipboardManager cm;
    private ClipData mClipData;
    private String money;
    private boolean flag;


    private void initView (View view) {
        myyongjin = view.findViewById(R.id.myyongjin);
        shareuserid = view.findViewById(R.id.shareuserid);
        todeyadd = view.findViewById(R.id.todeyadd);
        addnumberuser = view.findViewById(R.id.addnumberuser);
        todeyteamadd = view.findViewById(R.id.todeyteamadd);
        teamaddnumber = view.findViewById(R.id.teamaddnumber);
        yestodayget = view.findViewById(R.id.yestodayget);
        historymoney = view.findViewById(R.id.historymoney);
        cangetmoney = view.findViewById(R.id.cangetmoney);
        getmoney = view.findViewById(R.id.getmoney);
        getmoneyurl = view.findViewById(R.id.getmoneyurl);
        copeshareurl = view.findViewById(R.id.copeshareurl);
        copyyaoqingma = view.findViewById(R.id.copyyaoqingma);
        yaoqingma = view.findViewById(R.id.yaoqingma);

        getmoney.setOnClickListener(onClickListener);
        copeshareurl.setOnClickListener(onClickListener);
        copyyaoqingma.setOnClickListener(onClickListener);

    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.myrecommendlayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getpromotion();
        flag = false;
        cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    }

    @Override
    public void onHiddenChanged (boolean hidden) {
        super.onHiddenChanged(hidden);
        getpromotion();
    }

    private void getpromotion () {
        HttpJsonUtils.getHttpApiInstance()
                .getpromotion(Constants.getuseAdress() + "api/m/promotion/promotion", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<Respromotion>(MyrecommendFragment.this) {
                .subscribe(new CustomObserver<Respromotion>() {
                    @Override
                    protected void onCustomNext (Respromotion resActivity) {
                        if (resActivity.code == 0) {
                            myyongjin.setText("我的推荐码:" + resActivity.data.invitationCode);
                            shareuserid.setText("推荐人:" + resActivity.data.invitationId);
                            todeyadd.setText("今日直属新增:" + resActivity.data.todayDirectly);
                            addnumberuser.setText("直属成员人数:" + resActivity.data.historyTeam);
                            todeyteamadd.setText("今日团队新增:" + resActivity.data.todayTeam);
                            teamaddnumber.setText("团队成员人数:" + resActivity.data.todayDirectly);
                            yestodayget.setText("昨日可得:" + resActivity.data.yesterdayMoney);
                            historymoney.setText("历史总佣金:" + resActivity.data.historyDirectly);
                            cangetmoney.setText("可领取佣金:" + resActivity.data.todayMoney);
                            money = resActivity.data.todayDirectly;
                            yaoqingma.setText("" + resActivity.data.invitationCode);
                            getmoneyurl.setText(Constants.getuseAdress() + "m/#/?promoter=" + resActivity.data.userId);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.getmoney:
                    SYSOUtils.sOut_("提交金额为++++++++++++++++++" + money);
                    if (!money.equals("0")) {
                        if (!flag) {
                            getmoney();
                        } else {
                            Toast.makeText(getContext(), "请耐心等待，勿重复提交！", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(getContext(), "您没有额度,领取失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.copeshareurl:
                    mClipData = ClipData.newPlainText("Label", getmoneyurl.getText().toString().trim());
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.copyyaoqingma:
                    mClipData = ClipData.newPlainText("Label", yaoqingma.getText().toString().trim());
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    private void getmoney () {
        HttpJsonUtils.getHttpApiInstance()
                .getreceive(Constants.getuseAdress() + "api/m/promotion/receive", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResgetMoney>(MyrecommendFragment.this) {
                .subscribe(new CustomObserver<ResgetMoney>() {
                    @Override
                    protected void onCustomNext (ResgetMoney resActivity) {
                        if (resActivity.code == 0) {
                            Toast.makeText(getContext(), "领取成功", Toast.LENGTH_SHORT).show();
                        } else {
                            flag = true;
                            Toast.makeText(getContext(), "" + resActivity.msg, Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }
}
